package com.mfin.bom.factory;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.mfin.bom.adapter.DocChargeAdapter;
import com.mfin.bom.adapter.FinanceAdapter;
import com.mfin.bom.adapter.LoanMasterAdapter;
import com.mfin.bom.adapter.LoanPreferenceAdapter;
import com.mfin.bom.factory.interfaces.ILoanMasterFactory;
import com.mfin.bom.interfaces.IDocCharge;
import com.mfin.bom.interfaces.IFinance;
import com.mfin.bom.interfaces.ILoanMaster;
import com.mfin.bom.interfaces.ILoanPreference;
import com.mfin.bom.interfaces.IVehicle;
import com.mfin.bom.objects.DocCharge;
import com.mfin.bom.objects.LoanMaster;
import com.mfin.bom.objects.LoanPreference;
import com.mfin.bom.objects.LoanPreferenceId;
import com.mfin.enumeration.LoanType;
import com.mfin.vo.ObjectKey;
import com.mfin.vo.finance.FinanceVO;
import com.mfin.vo.interfaces.IIdentifiableObject;

public class LoanMasterFactory extends BaseFactory implements
		ILoanMasterFactory {

	@Override
	public ILoanMaster getLoanMaster(Serializable id) {
		ILoanMaster lm = null;

		try {
			LoanMaster l = (LoanMaster) getPersistenceGovernor().get(
					LoanMaster.class, id);

			if (l != null) {
				lm = new LoanMasterAdapter(l);
			}
		} catch (Exception ex) {
			// throw some exception
			ex.printStackTrace();
		}
		return lm;
	}

	@Override
	public List<ILoanMaster> getAllLoanTypes() {
		List<ILoanMaster> masterlist = null;

		try {
			List<LoanMaster> list = (List<LoanMaster>) getPersistenceGovernor()
					.createCriteria(LoanMaster.class);

			if (list != null) {
				masterlist = new ArrayList<ILoanMaster>();

				for (LoanMaster l : list) {
					masterlist.add(new LoanMasterAdapter(l));
				}

			}
		} catch (Exception ex) {
			// throw some exception
			ex.printStackTrace();
		}
		return masterlist;
	}

	@Override
	public ILoanPreference getFinanceLoanPreference(FinanceVO financeVO,
			LoanType loanType) {
		ILoanPreference lp = null;
		try {
			ObjectKey[] keys = financeVO.getKeys();
			LoanPreferenceId id = new LoanPreferenceId(loanType,
					(Long) keys[0].getId());
			LoanPreference l = (LoanPreference) getPersistenceGovernor().get(
					LoanPreference.class, id);

			if (l != null) {
				lp = new LoanPreferenceAdapter(l);
			}
		} catch (Exception ex) {
			// throw some exception
			ex.printStackTrace();
		}
		return lp;
	}

	@Override
	public ILoanPreference getFinanceLoanPreference(
			IIdentifiableObject identifiableObject) {
		ILoanPreference lp = null;
		try {
			ObjectKey[] keys = identifiableObject.getKeys();
			LoanPreference l = (LoanPreference) getPersistenceGovernor().get(
					LoanPreference.class, (LoanPreferenceId) keys[2].getId());

			if (l != null) {
				lp = new LoanPreferenceAdapter(l);
			}
		} catch (Exception ex) {
			// throw some exception
			ex.printStackTrace();
		}
		return lp;
	}

	@Override
	public IVehicle createVehicle() {
		IVehicle va = null;
		try {
			// Vehicle v = new Vehicle();

			// if (v != null) {
			// va = new VehicleAdapter(v);
			// }
		} catch (Exception ex) {
			// throw some exception
			ex.printStackTrace();
		}
		return va;
	}

	@Override
	public ILoanMaster createLoanMaster(LoanType loanType) {
		ILoanMaster lm = null;
		try {
			LoanMaster l = new LoanMaster();
			l.setLoanType(loanType);

			if (l != null) {
				getPersistenceGovernor().insert(l);
				lm = new LoanMasterAdapter(l);
			}
		} catch (Exception ex) {
			// throw some exception
			ex.printStackTrace();
		}
		return lm;
	}

	@Override
	public ILoanPreference createLoanPreference(IFinance bomFinance,
			ILoanMaster bomLoanMaster) {
		ILoanPreference lp = null;
		try {
			LoanPreferenceId id = new LoanPreferenceId(
					(LoanType) bomLoanMaster.getKeys()[0].getId(),
					(Long) bomFinance.getKeys()[0].getId());

			LoanPreference l = new LoanPreference();

			if (l != null) {
				l.setId(id);
				l.setFinance(((FinanceAdapter) bomFinance).getFinance());
				l.setLoanMaster(((LoanMasterAdapter) bomLoanMaster)
						.getLoanMaster());
				getPersistenceGovernor().insert(l);
				lp = new LoanPreferenceAdapter(l);
			}
		} catch (Exception ex) {
			// throw some exception
			ex.printStackTrace();
		}
		return lp;
	}

	@Override
	public IDocCharge createDocCharge() {
		IDocCharge dc = null;
		try {
			DocCharge d = new DocCharge();

			if (d != null) {
				getPersistenceGovernor().insert(d);
				dc = new DocChargeAdapter(d);
			}
		} catch (Exception ex) {
			// throw some exception
			ex.printStackTrace();
		}
		return dc;
	}

	@Override
	public void deleteDocChargeList(List<IDocCharge> bomDocCharges) {
		if (bomDocCharges != null) {
			for (IDocCharge bomDocCharge : bomDocCharges) {
				getPersistenceGovernor().delete(((DocChargeAdapter)bomDocCharge).getDocCharge());
			}
		}
	}
}
